<?php
declare (strict_types=1);

namespace app\course\service\base;

use app\admin\logic\system\Config as SystemConfig;
use app\common\exception\LogicException;
use app\common\logic\category\Category;
use app\common\service\BaseService;
use app\course\logic\base\Course as CourseLogic;
use app\course\logic\base\CourseSection;
use app\course\logic\base\CourseStudent;
use app\course\logic\base\SearchLog;
use app\course\logic\comment\Comment;
use app\course\logic\note\Note;
use app\course\logic\record\LearnRecord;
use app\course\model\video\CourseSectionVideo;
use eduline\upload\stocks\aliyun\File;
use think\annotation\Inject;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Event;
use think\facade\Request;
use think\Response;

/**
 * 课程接口类-前台
 */
class Course extends BaseService
{
    /**
     * @Inject()
     *
     * @var CourseLogic
     */
    protected $courseLogic;

    /**
     * @Inject()
     * @var LearnRecord
     */
    protected $learnRecord;

    /**
     * 课程列表
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-16
     *
     * @throws DbException
     */
    public function getCourseList(): Response
    {
        $data = $this->courseLogic->getCourseList();
        return static::parseToData($data, 1, '获取成功');
    }

    public function searchTitle(): Response
    {
        $param = Request::get();
        $data  = $this->courseLogic->searchTitle($param);
        return static::parseToData($data, 1, '获取成功');
    }

    public function hotSearch(): Response
    {
        $data = app(SearchLog::class)->hotSearch();
        return static::parseToData($data, 1, '获取成功');
    }

    /**
     * 课程详情
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-16
     */
    public function getCourseDetails($id): Response
    {
        $data = $this->courseLogic->getCourseDetails($id);

        $type = [
            1 => 'video',
            2 => 'live',
            3 => 'offline',
            4 => 'classes',
        ];

        if ($data['course_type'] == 2 && $data['course_live_type'] == 2) {
            $resource_type = 'live_small';
        } else {
            $resource_type = $type[$data['course_type']] ?? 'public';
        }

        Event::trigger('WebsiteVisit', [
            'mhm_id'        => $data['mhm_id'],
            'resource_type' => $resource_type,
            'resource_id'   => $id,
        ]);

        return static::parseToData($data, 1, '获取成功');
    }

    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function getCourseSplendid($id): Response
    {
        $data = $this->courseLogic->getCourseSplendid($id);

        return static::parseToData($data, 1, '获取成功');
    }

    /**
     * 获取指定章节
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-20
     *
     * @param    [type]                      $id [description]
     */
    public function getCourseAppointSections($id = 0, $pid = 0): Response
    {
        $classId = intval(Request::get('class_id'));
        $data    = $this->courseLogic->getCourseAppointSections($id, $pid, $classId);

        return static::parseToData($data, 1, '获取成功');
    }

    /**
     * 获取课时fileurl
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-08-20
     *
     * @param    [type]                      $id [description]
     */
    public function getCourseSectionFileurl($id = 0, $sid = 0): Response
    {
        // 参数
        $param    = Request::get();
        $class_id = $param['class_id'] ?? 0;

        $data = $this->courseLogic->getCourseSectionFileurl(intval($id), intval($sid), intval($class_id));

        return static::parseToData($data, 1, '获取成功');
    }

    public function getCourseSectionVideoYunParam($sid = 0, $stock = ''): Response
    {
        // 课时信息
        $secDataId = app(CourseSection::class)->whereId($sid)->value('course_data_id');
        $secData   = app(CourseSectionVideo::class)->with(['attach_savename'])->findOrFail($secDataId)->append([]);

        if ($secData->stock == 'local') throw new LogicException('非云端数据');

        $data['videoid'] = $secData->getAttr('savename');
        //
        switch ($stock) {
            case 'bokecc':
                $conf           = SystemConfig::get('system.package.upload.bokecc', ['userid' => '', 'apikey' => ''], request()->mhm_id);
                $data['userid'] = $conf['userid'];
                $data['apikey'] = $conf['apikey'];
                break;
            case 'aliyun':
                $conf              = SystemConfig::get('system.package.upload.aliyun', [], request()->mhm_id);
                $data['accId']     = $conf['hls_play_acc_id'] ?? ($conf['accessKey_id'] ?? '');
                $data['accSecret'] = $conf['hls_play_acc_secret'] ?? ($conf['accessKey_secret'] ?? '');
                $data['roleArn']   = $conf['hls_play_role_arn'] ?? '';
                $file              = new File();
                $data['palyAuth']  = $file->playAuth($data['videoid']);
            default:

        }

        return static::parseToData($data, 1, '获取成功');
    }

    /**
     * 获取课程分类
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-04-08
     */
    public function getCategoryTree(): Response
    {
        $tree = app(Category::class)->getTree();
        return static::parseToData($tree, 1, 'test');
    }

    /**
     * 获取开通的课程类型
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-04-08
     */
    public function getOpenType(): Response
    {
        $object = [
            'video'   => ['id' => 1, 'title' => '点播'],
            'live'    => ['id' => 2, 'title' => '直播'],
            // 'offline' => ['id' => 3, 'title' => '面授'],
            'classes' => ['id' => 4, 'title' => '班级'],
        ];

        return static::parseToData(array_values($object), 1, '获取成功');
    }

    /**
     * 我的点评
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-06-04
     */
    public function myCommentsList(): Response
    {
        $list = app(Comment::class)->myCommentsList();
        return static::parseToData($list, 1, '获取成功');
    }

    /**
     * 获取课程点评信息
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-04-10
     */
    public function getCourseComments($id): Response
    {
        $comments = app(Comment::class)->getList($id);
        return static::parseToData($comments, 1, '获取成功');
    }

    /**
     * 获取课程点评信息
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-04-10
     */
    public function addCourseComment($id): Response
    {
        $data    = Request::post();
        $comment = app(Comment::class)->addComment($id, $data);
        return static::parseToData($comment, 1, '评论成功');
    }

    /**
     * 删除课程评论
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 23/09/25
     *
     * @param $id
     * @return Response
     */
    public function deleteCourseComment($id)
    {
        app(Comment::class)->delComment($id);
        return static::parseToData([], 1, '删除成功');
    }

    /**
     * 点赞状态
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-04-17
     */
    public function changeCommentLikeStatus($id): Response
    {
        $data = Request::put();
        $re   = app(Comment::class)->changeCommentLikeStatus($id, $data);

        return static::parseToData($re, 1, '成功');
    }

    /**
     * 获取课程点评评论
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-04-10
     */
    public function getCourseCommentReply($id): Response
    {
        $reply = app(Comment::class)->getReply($id);
        return static::parseToData($reply, 1, '获取成功');
    }

    /**
     * 添加课程点评评论
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-04-10
     */
    public function addCourseCommentReply($id): Response
    {
        $data  = Request::post();
        $reply = app(Comment::class)->addReply($id, $data);
        return static::parseToData($reply, 1, '回复成功');
    }

    /**
     * 删除课程点评下的评论回复
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 23/09/25
     *
     * @param $id
     * @return Response
     */
    public function deleteCourseCommentReply($id)
    {
        app(Comment::class)->delCommentReply($id);
        return static::parseToData([], 1, '删除成功');
    }

    /**
     * 点赞状态
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-04-17
     */
    public function changeCommentReplyLikeStatus($id): Response
    {
        $data = Request::put();
        $re   = app(Comment::class)->changeCommentReplyLikeStatus($id, $data);

        return static::parseToData($re, 1, '成功');
    }

    /**
     * 获取课程笔记
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-04-13
     */
    public function getNotes(): Response
    {
        $param = Request::get();
        $notes = app(Note::class)->getList($param);
        return static::parseToData($notes, 1, '获取成功');
    }

    /**
     * 添加笔记
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-04-13
     */
    public function addNote(): Response
    {
        $param = Request::post();
        $note  = app(Note::class)->addNote($param);
        return static::parseToData($note, 1, '添加成功');
    }

    /**
     * 修改笔记
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-04-17
     */
    public function editNote($id): Response
    {
        $param = Request::put();
        $note  = app(Note::class)->editNote($id, $param);
        return static::parseToData($note, 1, '修改成功');
    }

    /**
     * 添加学习记录
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-13
     */
    public function addLearnRecord(): Response
    {
        $param = Request::post();
        request()->mid && $this->learnRecord->addLearnRecord($param);
        return static::parseToData([], 1, '提交成功');
    }

    /**
     * 学习界面
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-14
     */
    public function fetchRecord(): Response
    {
        $data = $this->learnRecord->fetchRecord();
        return static::parseToData($data, 1, '获取成功');
    }

    /**
     * 获取我加入的课程
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-18
     */
    public function getMyCourse(): Response
    {
        $param = Request::get();
        $data  = $this->learnRecord->getCourseList($param);
        return static::parseToData($data, 1, '获取成功');
    }

    /**
     * 获取我的学习记录列表
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-18
     */
    public function getMyRecord(): Response
    {
        $data = $this->learnRecord->getRecordList();
        return static::parseToData($data, 1, '获取成功');
    }

    /**
     * 免费课程加入学习
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-18
     */
    public function joinCourseStudent(): Response
    {
        $param = Request::post();
        app(CourseStudent::class)->joinCourseStudent($param);

        return static::parseToData([], 1, '加入成功');
    }

    /**
     * 查询课时解锁状态
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/12/27
     *
     * @return Response
     */
    public function isCourseSecUnlock(): Response
    {
        $param = Request::get();
        $data  = $this->courseLogic->isCourseSecUnlock($param);

        return static::parseToData($data, 1, '获取成功');
    }

    /**
     * 获取课程弹题数据
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2023-06-26
     */
    public function getCourseQuestion($id): Response
    {
        // 参数
        $param    = Request::get();
        $class_id = $param['class_id'] ?? 0;

        $data = $this->courseLogic->getCourseQuestion($id,$class_id);

        return static::parseToData($data, 1, '获取成功');
    }

    /**
     * 获取课程课件列表
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2023-06-26
     */
    public function getCourseWare($id): Response
    {
        $data = $this->courseLogic->getCourseWare($id);

        return static::parseToData($data, 1, '获取成功');
    }

    /**
     * 记录用户课件下载
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2023-07-06
     */
    public function saveCourseWareRecord(): Response
    {
        $param = Request::post();
        $this->courseLogic->saveCourseWareRecord($param);

        return static::parseToData([], 1, '添加成功');
    }
}
